<?php

namespace IDock\Insurance\AICS\Request;

use Exception;
use IDock\Insurance\AICS\AICSEnums;
use IDock\Insurance\AICS\Request\AllAICSRequest;
use IDock\Insurance\AICS\Response\UnderwritingResponse;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\SpecialClausesVisitor;

/**
 * 核保接口
 */
class UnderwritingRequest extends AllAICSRequest
{
    public $code = 'NewXnyUnderwritingService';

    public $envelopeName = 'new:underwriteXnyVehiclePolicyService';

    protected $response = UnderwritingResponse::class;

    protected $urlTip = '核保';

    /**
     * @var PremiumInquiryVisitor $visitor
     */
    protected $visitor;

    /**
     * @return array
     * @throws Exception
     */
    public function generate(): array
    {
        $data = [
            'condition' => [
                'deptCode' => $this->visitor->getExtInfoVisitor()->argc['CDptCde'] ?? '',
                'jqFlag' => '0',
                'syFlag' => '0',
                'ngo' => '',//总牵引质量（kg
                'TResvTm' => $this->visitor->getVehicleVisitor()->issueDate,//发证日期
                'cScsyxz' => AICSEnums::$useNatureCode[$this->visitor->getVehicleVisitor()->useNatureCode] ?? '',//四川平台车辆使用性质
                'cProdNoSY' => '',//商业险产品号
                'cProdNoJQ' => '',//交强险产品号
                'querySequenceNoJq' => $this->visitor->getExtInfoVisitor()->argc['CQryCdeJQ'] ?? '',//投保交强险必传
                'querySequenceNoSy' => $this->visitor->getExtInfoVisitor()->argc['CQryCdeSY'] ?? '',//投保商业险必传
                'syAppNo' => $this->visitor->getExtInfoVisitor()->argc['syAppNo'] ?? '',//商业险投保单号 退回修改单时必传
                'jqAppNo' => $this->visitor->getExtInfoVisitor()->argc['jqAppNo'] ?? '',//交强险投保单号 退回修改单时必传
                'taxTaxableQuantity' => '1',//计税单位的数量
                'taxUsageCde' => $this->visitor->getVehicleVisitor()->useNatureCode == '01' ? '200' : '100',//使用性质
                'customer' => $this->getCustomer(),
                'fixJqlist' => $this->getJqList(),
                'fixSylist' => $this->getBiList(),
                'taxATion' => $this->getTaxation(),
            ],
        ];
        if (empty($data['condition']['fixJqlist']['specContent'])) {
            unset($data['condition']['fixJqlist']);
        }
        if (empty($data['condition']['fixSylist']['specContent'])) {
            unset($data['condition']['fixSylist']);
        }
        if (!empty($this->visitor->ciStartData)) {
            $data['condition']['jqFlag'] = '1';
            $data['condition']['cProdNoJQ'] = '0326';
            if (empty($data['condition']['querySequenceNoJq'])) {
                throw new Exception("交强险查询码不能为空");
            }
        }
        if (!empty($this->visitor->biStartData)) {
            $data['condition']['syFlag'] = '1';
            $data['condition']['cProdNoSY'] = $this->visitor->getVehicleVisitor()->isNewEnergy ? '0369' : '0364';
            if (empty($data['condition']['querySequenceNoSy'])) {
                throw new Exception("商业险查询码不能为空");
            }
        }
        if(!empty($this->visitor->getNonCarUnderwritingVisitor()->getNonCarList())){
            $data['condition']['ywFlag'] = '1';//驾意险flag
        }

        return $data;
    }

    public function getCustomer()
    {
        return [
            'benefitCustType' => '',
            'benefitCertoType' => '',
            'benefitCertiCode' => '',
            'benefitAge' => '',
            'benefitSex' => '',
            'benefitName' => '',
            'bnfctcertfclsvalidate' => $this->visitor->getApplicantVisitor()->certificateEndDate,
            'ownerAddress' => $this->visitor->getOwnerVisitor()->address,
            'appcIncome' => '',
            'insuredcIncome' => '',
            'appCertfClsStart' => $this->visitor->getApplicantVisitor()->certificateStartDate,
            'insuredCertfClsStart' => $this->visitor->getInsuredVisitor()->certificateStartDate,
            'CProvince' => $this->visitor->getApplicantVisitor()->provinceCode,
            'CCity' => $this->visitor->getApplicantVisitor()->cityCode,
            'CCounty' => $this->visitor->getApplicantVisitor()->areaCode,
            'cZipCde' => '',
        ];
    }

    public function getJqList()
    {
        $list = $this->visitor->getUnderwritingVisitor()->specialClausesList;
        $ciList = [];
        /**@var SpecialClausesVisitor $val * */
        foreach ($list as $val) {
            if ($val->riskType == 'TCI') {
                $ciList[] = $val->clauseContent;
            }
        }
        return ['specContent' => $ciList];
    }

    public function getBiList()
    {
        $list = $this->visitor->getUnderwritingVisitor()->specialClausesList;
        $biList = [];
        /**@var SpecialClausesVisitor $val * */
        foreach ($list as $val) {
            if ($val->riskType == 'VCI') {
                $biList[] = $val->clauseContent;
            }
        }
        return ['specContent' => $biList];
    }

    private function getTaxation(): array
    {
        return [
            'taxName' => $this->visitor->getApplicantVisitor()->name,
            'cTaxType' => $this->visitor->getApplicantVisitor()->typeCode == 'R1' ? '338001' : '338003',//纳税人类型
            'clntMark' => '2',//纳税人身份 增值税一般纳税人
            'taxpayerComid' => $this->visitor->getApplicantVisitor()->certificateNo,//纳税人识别号
            'clntAddress' => $this->visitor->getApplicantVisitor()->address,//地址
            'mobile' => $this->visitor->getApplicantVisitor()->mobile,//mobile
            'bankName' => '',//bankName
            'acctNo' => '',//acctNo
        ];
    }

}
